<?php

    /* Local Functions */
    function echoPlayerRow($player, $id, $race, $usesExp)
    {
        echo "<tr align=center>\n";
        echo "<input type=\"hidden\" name=\"player_id" . $id . "\" value=\"$player->playerId\">\n";
        echo "<td><select name=\"roster_num" . $id . "\">";
        for ($i = 1; $i < 17; $i++)
        {
            echo "<option value=\"$i\"";
            if ($i == $id)
            {
                echo " selected";
            }
            echo ">$i</option>";
        }
        echo "</select></td>\n";

        echo "<td align=left class=\"small\"><input name=\"name" . $id . "\" type=\"text\" size=\"22\" maxlength=\"64\" value=\"" . $player->name . "\"/></td>\n";


        echo "<td align=left class=\"small\">";
        $hasPosition = false;
        foreach($race->positions as $position)
        {
            if (0 == strcasecmp($player->position, $position->position))
            {
                $hasPosition = true;
                break;
            }
        }

        if (!$hasPosition and isset($player->position))
        {
            echo "<input name=\"position" . $id . "\" size=\"16\" value=\"" . $player->position . "\"></td>\n";
        }
        else
        {
            echo "<select name=\"position" . $id . "\" STYLE=\"width: 110px\" onChange=\"updateRow('$id')\">\n<option value=\"\" selected>&nbsp;</option>\n";
            foreach($race->positions as $position)
            {
                echo "<option value=\"" . $position->position . "\"";
                if (0 == strcasecmp($player->position, $position->position))
                {
                    echo " selected";
                }
                echo ">" . $position->position . "</option>\n";
            }
            echo "</select></td>";
        }

        echo "<td><input name=\"ma" . $id . "\" type=\"text\" size=\"2\" value=\"" . $player->ma . "\"/></td>\n";
        echo "<td><input name=\"st" . $id . "\" type=\"text\" size=\"2\" value=\"" . $player->st . "\"/></td>\n";
        echo "<td><input name=\"ag" . $id . "\" type=\"text\" size=\"2\" value=\"" . $player->ag . "\"/></td>\n";
        echo "<td><input name=\"av" . $id . "\" type=\"text\" size=\"2\" value=\"" . $player->av. "\"/></td>\n";
        echo "<td align=left class=\"small\"><input name=\"skills" . $id . "\" type=\"text\" size=\"30\" maxlength=\"128\" value=\"" . $player->skills . "\"/></td>\n";
        echo "<td><select name=\"mng" . $id . "\" onChange=\"updatePlayerCosts()\">";
        if ($player->injured)
        {
            echo "<option value=\"false\">&nbsp;</option>";
            echo "<option value=\"true\" selected>mng</option>";
        }
        else
        {
            echo "<option value=\"false\" selected>&nbsp;</option>";
            echo "<option value=\"true\">mng</option>";
        }
        echo "</select></td>\n";
        echo "<td><input name=\"nig" . $id . "\" type=\"text\" size=\"2\" value=\"" . $player->niggles . "\"/></td>\n";
        echo "<td><input name=\"cp" . $id . "\" type=\"text\" size=\"2\" onChange=\"updateSPPs('" . $id . "')\" value=\"" . $player->completions . "\"/></td>\n";
        echo "<td><input name=\"td" . $id . "\" type=\"text\" size=\"2\" onChange=\"updateSPPs('" . $id . "')\" value=\"" . $player->tds . "\"/></td>\n";
        echo "<td><input name=\"in" . $id . "\" type=\"text\" size=\"2\" onChange=\"updateSPPs('" . $id . "')\" value=\"" . $player->interceptions . "\"/></td>\n";
        echo "<td><input name=\"cas" . $id . "\" type=\"text\" size=\"2\" onChange=\"updateSPPs('" . $id . "')\" value=\"" . $player->casualties . "\"/></td>\n";
        if ($usesExp)
        {
            echo "<td><input name=\"exp" . $id . "\" type=\"text\" size=\"2\" onChange=\"updateSPPs('" . $id . "')\" value=\"" . $player->exps. "\"/></td>\n";
        }
        else
        {
            echo "<td><input name=\"mvp" . $id . "\" type=\"text\" size=\"2\" onChange=\"updateSPPs('" . $id . "')\" value=\"" . $player->mvps . "\"/></td>\n";
        }

        echo "<td><input name=\"spps" . $id . "\" type=\"text\" size=\"2\" onfocus=\"changeFocus($id)\" value=\"" . (!$player ? "" : $player->getSPPs()) . "\"/></td>\n";
        echo "<td align=\"right\"><input name=\"cost" . $id . "\" type=\"text\" size=\"2\" onChange=\"updatePlayerCosts()\" value=\"" . $player->cost . "\"/>&nbsp;k</td>\n";
        echo "</tr>\n";
    }

    function echoHomeMatch($match, $team, $opponent)
    {
        echo "<tr align=\"middle\">\n<td><form method=\"post\">";
        echo "<input type=\"hidden\" name=\"id\" value=" . $team->id . ">\n";
        echo "<input type=\"hidden\" name=\"mrid\" value=" . $match->homeRec->id . ">\n";
        if ($match->homeRec->score > $match->awayRec->score)
        {
            echo "Won";
        }
        else if ($match->homeRec->score == $match->awayRec->score)
        {
            echo "Drawn";
        }
        else
        {
            echo "Lost";
        }
        echo "</td>\n<td>" . $opponent->coach->name . "</td>\n";
        echo "<td>" . $opponent->name . "</td>\n";
        echo "<td>" . $match->homeRec->score . "</td>\n";
        echo "<td>" . $match->awayRec->score . "</td>\n";
        echo "<td>" . $match->homeRec->casualties . "</td>\n";
        echo "<td>" . $match->awayRec->casualties . "</td>\n";
        echo "<td>" . $match->homeRec->winnings . "k</td>\n";
        echo "<td>" . getFanText($match->homeRec->fanFactor) . "</td>\n";
        echo "<td vAlign=\"top\" align=\"left\"><textarea name=\"comments\" rows=\"4\" cols=\"55\">" . $match->homeRec->comments . "</textarea></td>\n";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Update\nMatch\"></td></form></tr>\n";
    }

    function echoAwayMatch($match, $team, $opponent)
    {
        echo "<tr align=\"middle\">\n<td><form method=\"get\">\n";
        echo "<input type=\"hidden\" name=\"id\" value=" . $team->id . ">\n";
        echo "<input type=\"hidden\" name=\"mrid\" value=" . $match->awayRec->id . ">\n";
        if ($match->homeRec->score < $match->awayRec->score)
        {
            echo "Won";
        }
        else if ($match->homeRec->score == $match->awayRec->score)
        {
            echo "Drawn";
        }
        else
        {
            echo "Lost";
        }
        echo "</td>\n<td>" . $opponent->coach->name . "</td>\n";
        echo "<td>" . $opponent->name . "</td>\n";
        echo "<td>" . $match->awayRec->score . "</td>\n";
        echo "<td>" . $match->homeRec->score . "</td>\n";
        echo "<td>" . $match->awayRec->casualties . "</td>\n";
        echo "<td>" . $match->homeRec->casualties . "</td>\n";
        echo "<td>" . $match->awayRec->winnings . "k</td>\n";
        echo "<td>" . getFanText($match->awayRec->fanFactor) . "</td>\n";
        echo "<td vAlign=\"top\" align=\"left\"><textarea name=\"comments\" rows=\"4\" cols=\"55\">" . $match->awayRec->comments . "</textarea></td>\n";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Update\nMatch\"></td></form></tr>\n";
    }

    /* End Local Functions */

    include("includes/init.php");

	$id = getParam("id");
    $team = $teams->getTeam($id);
    $reload = false;

	$submit = getParam("submit");
	$mrid = getParam("mrid");
	$comments = getParam("comments");
	
    if (isset($submit))
    {
        if (isset($mrid)) {
        	$mrd = new MatchRecordDao();
        	$matchRecord = $mrd->load($mrid);
        	print_r($matchRecord);
            if ($loggedin->canEditComment($matchRecord)) {
            	$matchRecord->comments = stripslashes($comments);
                $reload = $matches->updateMatchRecord($mrid, $comments);
	        	$matchRecord->update();
            }
        }
        else if ($loggedin->canUpdateTeam($team))
        {
            for ($j = 1; $j < 17; $j++)
            {
                $team->updatePlayer(getVariable("player_id", $j, $_POST),
                                    getVariable("roster_num", $j, $_POST),
                                    getVariable("name", $j, $_POST),
                                    getVariable("position", $j, $_POST),
                                    getVariable("ma", $j, $_POST),
                                    getVariable("st", $j, $_POST),
                                    getVariable("ag", $j, $_POST),
                                    getVariable("av", $j, $_POST),
                                    getVariable("skills", $j, $_POST),
                                    getVariable("mng", $j, $_POST),
                                    getVariable("nig", $j, $_POST),
                                    getVariable("cp", $j, $_POST),
                                    getVariable("td", $j, $_POST),
                                    getVariable("in", $j, $_POST),
                                    getVariable("cas", $j, $_POST),
                                    getVariable("mvp", $j, $_POST),
                                    getVariable("exp", $j, $_POST),
                                    getVariable("cost", $j, $_POST));
            }

            $error = $teams->updateFullTeam($team,
                                            getParam("team_name"),
                                            getRace(getParam("race_id")),
                                            getParam("treasury"),
                                            getParam("money_lent"),
                                            getParam("money_borrowed"),
                                            getParam("rerolls"),
                                            getParam("rerollCost"),
                                            getParam("fanfactor"),
                                            getParam("coaches"),
                                            getParam("cheerleaders"),
                                            getParam("apothecary"),
                                            getParam("text_colour"),
                                            getParam("background_colour"),
                                            getParam("title_background_colour"),
                                            getParam("title_text_colour"),
                                            getParam("light_border"),
                                            getParam("dark_border"));

            $team = $teams->getTeam($id);
            $team->save();
            $reload = true;
        }

        if ($reload)
        {
            $teams = new Teams();
            $md = new MatchDao();
            $matches = $md->findSeasonMatches($season);
			$_SESSION["teams"] = $teams;
			$_SESSION["matches"] = $matches;
        }
    }
    $team = $teams->getTeam($id);
	$md = new MatchDao();
	$teamMatches = $md->findTeamMatches($team->id);
    $numPositions = sizeof($team->race->positions);
    if ($team->usesExp)
    {
        $mvpMultiple = 1;
        $mvpTitle = "exp";
    }
    else
    {
        $mvpMultiple = 5;
        $mvpTitle = "mvp";
    }

    $rerollCost = $team->rerolls * $team->race->rerollCost;
    $costPerReroll = $team->race->rerollCost;
    $ffCost = $team->fanFactor * 10;
    $coachCost = $team->coaches * 10;
    $coachName = $team->coach->name;
    $cheerCost = $team->cheerleaders * 10;
    $apocCost = $team->apothecary * 50;
    $tr = $team->getTR();
    $totalCost = $team->getTotalCost();
echo <<< EOQ
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
  <head>
    <title>$team->name</title>
    <meta content="text/html; charset=windows-1252" http-equiv=Content-Type>
    <meta name="description" content="$defaultPageTitle">
    <meta name="keywords" content="bloodbowl, blood bowl, league, games workshop, games, bbl, blood, bowl, $keywords">
    <meta http-equiv="Cache-Control" content="max-age=0">
    <meta http-equiv="Pragma" content="no-cache">
    <script language="javascript">
    // nasty java script to update spp total and player stats
    var PositionSubscript = 0       //Identifies subscript of selected product in current row.
    var mvpMultiple = $mvpMultiple;

    function changeFocus(rowNum)
    {
        var exeLine='document.playerform.cost' + rowNum + '.focus()'
        eval (exeLine);
        var exeLine='document.playerform.cost' + rowNum + '.select()'
        eval (exeLine);
    }

    //-- Function to create a new, empty array that starts at zero.
    function BuildZeroArray(n)
    {
       this.length = n
       for (var i=0;i<=n;i++) {this[i]=0}
       return this
    }

    // class to represent a position in Javascript
    function position(name, ma, st, ag, av, skills, cost)
    {
       this.name = name
       this.ma = ma
       this.st = st
       this.ag = ag
       this.av = av
       this.skills = skills
       this.cost = cost
    }

    function getPlayerValue(number) {
        var mng = getValue('document.playerform.mng' + number + '.value')

        var cost = 0;
        if (!mng) {
            cost +=getValue('document.playerform.cost' + number + '.value')
        }
        return cost;

    }
    // update the total cost of players
    function updatePlayerCosts()
    {
        var cost = getPlayerValue('1');
        cost += getPlayerValue('2');
        cost += getPlayerValue('3');
        cost += getPlayerValue('4');
        cost += getPlayerValue('5');
        cost += getPlayerValue('6');
        cost += getPlayerValue('7');
        cost += getPlayerValue('8');
        cost += getPlayerValue('9');
        cost += getPlayerValue('10');
        cost += getPlayerValue('11');
        cost += getPlayerValue('12');
        cost += getPlayerValue('13');
        cost += getPlayerValue('14');
        cost += getPlayerValue('15');
        cost += getPlayerValue('16');

      var exeLine = 'document.playerform.playercost.value=cost'
      eval (exeLine)
      updateTeamCost()
    }

    // update the total spps of players
    function updatePlayerSpps()
    {
      var cost = getValue('document.playerform.spps1.value')
      cost+=getValue('document.playerform.spps2.value')
      cost+=getValue('document.playerform.spps3.value')
      cost+=getValue('document.playerform.spps4.value')
      cost+=getValue('document.playerform.spps5.value')
      cost+=getValue('document.playerform.spps6.value')
      cost+=getValue('document.playerform.spps7.value')
      cost+=getValue('document.playerform.spps8.value')
      cost+=getValue('document.playerform.spps9.value')
      cost+=getValue('document.playerform.spps10.value')
      cost+=getValue('document.playerform.spps11.value')
      cost+=getValue('document.playerform.spps12.value')
      cost+=getValue('document.playerform.spps13.value')
      cost+=getValue('document.playerform.spps14.value')
      cost+=getValue('document.playerform.spps15.value')
      cost+=getValue('document.playerform.spps16.value')
      var exeLine = 'document.playerform.playerspps.value=cost'
      eval (exeLine)
    }

    // update a row when position changes
    function updateRow(rownum)
    {
       var exeLine='PositionSubscript=document.playerform.position'+rownum+'.selectedIndex'
       eval(exeLine)
       var exeLine='document.playerform.ma'+rownum+'.value=positionList[PositionSubscript].ma'
       eval (exeLine)
       var exeLine='document.playerform.st'+rownum+'.value=positionList[PositionSubscript].st'
       eval (exeLine)
       var exeLine='document.playerform.ag'+rownum+'.value=positionList[PositionSubscript].ag'
       eval (exeLine)
       var exeLine='document.playerform.av'+rownum+'.value=positionList[PositionSubscript].av'
       eval (exeLine)
       var exeLine='document.playerform.skills'+rownum+'.value=positionList[PositionSubscript].skills'
       eval (exeLine)
       var exeLine='document.playerform.cost'+rownum+'.value=positionList[PositionSubscript].cost'
       eval (exeLine)
       updatePlayerCosts()
       updatePlayerSpps()
    }

    function getValue(exeLine)
    {
      var strValue = '';
      var intValue = 0;
      eval ('strValue='+exeLine)
      if (strValue.length != 0) {
        intValue = eval(strValue);
      }
      return intValue;
    }

    // update player SPP totals when one value changed
    function updateSPPs(rownum)
    {
      var spps = getValue('document.playerform.cp'+rownum+'.value')
      spps+=(3 * getValue('document.playerform.td'+rownum+'.value'))
      spps+=(2 * getValue('document.playerform.in'+rownum+'.value'))
      spps+=(2 * getValue('document.playerform.cas'+rownum+'.value'))
      spps+=(mvpMultiple * getValue('document.playerform.$mvpTitle'+rownum+'.value'))
      var exeLine='document.playerform.spps'+rownum+'.value=spps'
      eval (exeLine)
      updatePlayerSpps()
    }

    // updates total cost of team rerolls
    function updateRerolls()
    {
      var count = $costPerReroll * getValue('document.playerform.rerolls.value')
      var exeLine='document.playerform.totalRerollCost.value=count'
      eval (exeLine)
      updateTeamCost()
    }

    // updates total cost of fan factor
    function updateFFCost()
    {
      var count = 10 * getValue('document.playerform.fanfactor.value')
      var exeLine='document.playerform.ffCost.value=count'
      eval (exeLine)
      updateTeamCost()
    }

    // updates total cost of coaches
    function updateCoachCost()
    {
      var count = 10 * getValue('document.playerform.coaches.value')
      var exeLine='document.playerform.coachCost.value=count'
      eval (exeLine)
      updateTeamCost()
    }

    // updates total cost of cheerleaders
    function updateCheerCost()
    {
      var count = 10 * getValue('document.playerform.cheerleaders.value')
      var exeLine='document.playerform.cheerCost.value=count'
      eval (exeLine)
      updateTeamCost()
    }

    // updates total cost of an apothecary
    function updateApothCost()
    {
      var count = 50 * getValue('document.playerform.apothecary.value')
      var exeLine='document.playerform.apothCost.value=count'
      eval (exeLine)
      updateTeamCost()
    }

    // updates total cost of the team
    function updateTeamCost()
    {
      var count = getValue('document.playerform.apothCost.value')
      count+= getValue('document.playerform.cheerCost.value')
      count+= getValue('document.playerform.coachCost.value')
      count+= getValue('document.playerform.ffCost.value')
      count+= getValue('document.playerform.totalRerollCost.value')
      count+= getValue('document.playerform.playercost.value')

      var exeLine='document.playerform.teamCost.value=count'
      eval (exeLine)
      updateTeamRating()
    }

    // updates team rating
    function updateTeamRating()
    {
      var count = (getValue('document.playerform.teamCost.value'))
      // count+= (getValue('document.playerform.treasury.value'))
      // count+= (getValue('document.playerform.money_lent.value'))

      var exeLine='document.playerform.team_rating.value=count'
      eval (exeLine)
    }

    </script>
  </head>
  <body bgColor="$team->background" text="$team->text" vLink="$team->text" link="$team->text">
    <script language="JavaScript">
    //Create a new array named positionList with some elements.
    positionList = new BuildZeroArray($numPositions)   //Create empty product list array.
    positionList[0] = new position('', '', '', '', '', '', '')

EOQ;
  $posNum = 1;
  foreach($team->race->positions as $position) {
    echo "positionList[" . $posNum++ . "] = new position('" . $position->position
      . "', " . $position->ma
      . ", " . $position->st
      . ", " . $position->ag
      . ", " . $position->av
      . ", '" . $position->skillsStr
      . "', " . $position->cost
      . ")\n";
  }
echo <<< EOQ
    </script>
    <center>
      <font face="MS Sans Serif" size="4">
        <h1><a href="team_viewer.php?id=$team->id">$team->name</a></h1>
        <a href="league.php">Back to $leagueNameShort</a>
        <form name="playerform" action="team_editor.php" method="post">
        <input type="hidden" name="id" value="$team->id">
        <table border="1" width="100%" borderColorLight=$team->borderLight borderColorDark=$team->borderDark>
          <tr style="background-color: $team->titleBackground; color: $team->titleText;">
            <th>#</th>
            <th>Player Name</th>
            <th>Position</th>
            <th>ma</th>
            <th>st</th>
            <th>ag</th>
            <th>av</th>
            <th>Skills</th>
            <th>mng</th>
            <th>nig</th>
            <th>cp</th>
            <th>td</th>
            <th>in</th>
            <th>cs</th>
            <th>$mvpTitle</th>
            <th>spps</th>
            <th>Cost</th>
          </tr>
EOQ;
  $i = 0;
  for($i = 1; $i < 17; $i++) {
    echoPlayerRow($team->getPlayer($i), $i, $team->race, $team->usesExp);
  }
  $totalSPPs = $team->getSPPsTotal();
  $totalPlayerCost = $team->getPlayerCostTotal();
echo <<< EOQ
          <tr>
            <td colSpan="7">&nbsp;</td>
            <td colSpan="6" align="center">Totals</td>
            <td align="right" colSpan="3"><input type="text" name="playerspps" value="$totalSPPs" size="5" onFocus="document.playerform.team_name.focus();document.playerform.team_name.select()"></td>
            <td align="right"><input type="text" name="playercost" value="$totalPlayerCost" size="4" onFocus="document.playerform.team_name.focus();document.playerform.team_name.select()">&nbsp;k</td>
          </tr>

          <tr align="center">
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2" rowSpan="2">Team Name:</td>
            <td colSpan="5" rowSpan="2"><input type="text" name="team_name" value="$team->name" size="32" maxsize="64"/></td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="3">Re-Rolls:</td>
            <td colSpan="2"><input type="text" name="rerolls" value="$team->rerolls" size="2" onChange="updateRerolls()" /></td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x $costPerReroll k</td>
            <td align="right"><input type="text" name="totalRerollCost" value="$rerollCost" size="3" onFocus="document.playerform.fanfactor.focus();document.playerform.fanfactor.select()"/>&nbsp;k</td></tr>
          <tr align="middle">
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="3">Fan Factor:</td>
            <td colSpan="2"><input type="text" name="fanfactor" value="$team->fanFactor" size="2" onChange="updateFFCost()" /></td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 0 k =</td>
            <td align="right"><input type="text" name="ffCost" value="$ffCost" size="3" onFocus="document.playerform.coaches.focus();document.playerform.coaches.select()"/>&nbsp;k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Race:</td>
            <td colSpan=5>
EOQ;
echoRaceSelect($races, $team->race);
echo <<< EOQ
            </td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="3">Assistant Coaches:</td>
            <td colSpan="2"><input type="text" name="coaches" value="$team->coaches" size="2" onChange="updateCoachCost()" /></td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 10 k =</td>
            <td align="right"><input type="text" name="coachCost" value="$coachCost" size="3" onFocus="document.playerform.treasury.focus();document.playerform.treasury.select()"/>&nbsp;k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Treasury:</td>
            <td colSpan=5><input type="text" name="treasury" value="$team->treasury" size="3" onChange="updateTeamRating()" /> k</td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="3">Cheerleaders:</td>
            <td colSpan="2"><input type="text" name="cheerleaders" value="$team->cheerleaders" size="2" onChange="updateCheerCost()" /></td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 10 k =</td>
            <td align="right"><input type="text" name="cheerCost" value="$cheerCost" size="3" onFocus="document.playerform.money_lent.focus();document.playerform.money_lent.select()"/>&nbsp;k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Money Lent:</td>
            <td colSpan=5><input type="text" name="money_lent" value="$team->moneyLent" size="3" onChange="updateTeamRating()" /> k</td>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="3">Apothecary:</td>
            <td colSpan="2"><input type="text" name="apothecary" value="$team->apothecary" size="2" onChange="updateApothCost()" /></td>
            <td style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="4">x 50 k =</td>
            <td align="right"><input type="text" name="apothCost" value="$apocCost" size="3" onFocus="document.playerform.money_borrowed.focus();document.playerform.money_borrowed.select()"/>&nbsp;k</td></tr>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Money Borrowed:</td>
            <td colSpan=5><input type="text" name="money_borrowed" value="$team->moneyBorrowed" size="3" /> k</td>
            <td align="right" rowspan="2" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="9">TOTAL COST OF TEAM:</td>
            <td align="right" rowspan="2"><input type="text" name="teamCost" value="$totalCost" size="4" onFocus="document.playerform.background_colour.focus();document.playerform.background_colour.select()"/>&nbsp;k</td>
          <tr align=middle>
            <td align="right" style="background-color: $team->titleBackground; color: $team->titleText;" colSpan="2">Team Value:</td>
            <td colSpan=5><input type="text" name="team_rating" value="$tr" size="4" onFocus="document.playerform.background_colour.focus();document.playerform.background_colour.select()"/>&nbsp;k</td>
          </tr>
          </tbody>
        </table>
          <p>
          <p><h3>Team Colours</h3>
          <p>
        <table border="1" borderColorLight=$team->borderLight borderColorDark=$team->borderDark>
          <tr>
            <th style="background-color: $team->titleBackground; color: $team->titleText;">Background Colour</th>
            <th style="background-color: $team->titleBackground; color: $team->titleText;">Text Colour</th>
            <th style="background-color: $team->titleBackground; color: $team->titleText;">Title Background Colour</th>
            <th style="background-color: $team->titleBackground; color: $team->titleText;">Title Text Colour</th>
            <th style="background-color: $team->titleBackground; color: $team->titleText;">Light Border Colour</th>
            <th style="background-color: $team->titleBackground; color: $team->titleText;">Dark Border Colour</th>
          </tr>
          <tr>
            <td><input type="text" name="background_colour" value="$team->background" /></td>
            <td><input type="text" name="text_colour" value="$team->text" /></td>
            <td><input type="text" name="title_background_colour" value="$team->titleBackground" /></td>
            <td><input type="text" name="title_text_colour" value="$team->titleText" /></td>
            <td><input type="text" name="light_border" value="$team->borderLight" /></td>
            <td><input type="text" name="dark_border" value="$team->borderDark" /></td>
          </tr>
        </table>
        <center>
        <br /><br /><input type="submit" name="submit" value="Apply Changes and Save"></center>
        </form>
          <p>
          <p><h2>Match Record</h2>
          <p>
        <table border="1" width="100%" borderColorLight=$team->borderLight borderColorDark=$team->borderDark>
          <tr align=middle style="background-color: $team->titleBackground; color: $team->titleText;" >
            <th rowSpan=2>Result</th>
            <th rowSpan=2>Opponent</th>
            <th rowSpan=2>Team</th>
            <th colSpan="2">Score</th>
            <th colSpan="2">Cas</th>
            <th rowSpan=2>Winnings</th>
            <th rowSpan=2>Fan Factor</th>
            <th rowSpan=2>Notes &amp; Highlights</th>
            <th rowSpan=2>&nbsp;</th>
          </tr>
          <tr align=middle style="background-color: $team->titleBackground; color: $team->titleText;" >
            <th>For</th>
            <th>Ag</th>
            <th>For</th>
            <th>Ag</th>
          </tr>
EOQ;
  foreach ($teamMatches as $match) {
    if ($match->homeRec->teamId == $team->id) {
      echoHomeMatch($match, $team, $teams->getTeam($match->awayRec->teamId));
    } else {
      echoAwayMatch($match, $team, $teams->getTeam($match->homeRec->teamId));
    }
  }
echo <<< EOQ
        </table>
      </font>
    </center>
  </body>
</html>
EOQ;
?>